------------------------------------------------------------------------
-- Basic lookups and filters
------------------------------------------------------------------------

-- List all countries in Asia with their capital.
SELECT co.[name], co.[capital]
FROM [country] co
JOIN [continent] ct ON co.[continent_id] = ct.[id]
WHERE ct.[name] = 'Asia'
ORDER BY co.[name];

-- List every Olympic edition (year, host city, host country), most recent first.
SELECT o.`year`, o.[host_city], c.[name] AS [host_country]
FROM [olympic_games] o
JOIN [country] c ON o.[country_iso3] = c.[iso3]
ORDER BY o.`year` DESC;

-- Which events belong to the "Swimming" discipline? (most events exist for both
-- men and women, so the event gender is shown)
SELECT e.[name], g.[gender], e.[no_of_players]
FROM [event] e
JOIN [event_gender] g ON e.[event_gender_code] = g.[code]
WHERE e.[discipline] = 'Swimming'
ORDER BY e.[name], g.[gender];

-- List the stadiums in the United States, largest first.
SELECT s.[name], s.[seats], s.[address]
FROM [stadium] s
JOIN [country] c ON s.[country_iso3] = c.[iso3]
WHERE c.[name] = 'United States of America'
ORDER BY s.[seats] DESC;

-- How many athletes did each country send? (top 10)
SELECT c.[name], COUNT(a.[id]) AS [athletes]
FROM [athlete] a
JOIN [country] c ON a.[country_iso3] = c.[iso3]
GROUP BY c.[name]
ORDER BY [athletes] DESC
LIMIT 10;

------------------------------------------------------------------------
-- Medal analytics
------------------------------------------------------------------------
-- Note: athlete_result has one row per medal-winning ATHLETE, so these counts are
-- athlete medals -- a relay or team gold counts once for every team member.

-- Overall medal table: total medals by country. (top 15)
SELECT c.[name] AS [country], COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY c.[name]
ORDER BY [medals] DESC
LIMIT 15;

-- Gold / Silver / Bronze breakdown by country. (top 15 by gold)
SELECT c.[name] AS [country],
       SUM(CASE WHEN m.[type] = 'Gold'   THEN 1 ELSE 0 END) AS [gold],
       SUM(CASE WHEN m.[type] = 'Silver' THEN 1 ELSE 0 END) AS [silver],
       SUM(CASE WHEN m.[type] = 'Bronze' THEN 1 ELSE 0 END) AS [bronze]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY c.[name]
ORDER BY [gold] DESC, [silver] DESC
LIMIT 15;

-- Total medals won by each continent.
SELECT ct.[name] AS [continent], COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [continent] ct ON c.[continent_id] = ct.[id]
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY ct.[name]
ORDER BY [medals] DESC;

-- Medal table for the 2004 (Athens) Olympics. (top 10)
SELECT c.[name] AS [country], COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [medal] m ON oa.[medal_code] = m.[code]
WHERE oa.`year` = 2004
GROUP BY c.[name]
ORDER BY [medals] DESC
LIMIT 10;

-- Which countries have won the most gold medals all-time? (top 10)
SELECT c.[name], COUNT(*) AS [golds]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [medal] m ON oa.[medal_code] = m.[code]
WHERE m.[type] = 'Gold'
GROUP BY c.[name]
ORDER BY [golds] DESC
LIMIT 10;

-- Which disciplines have awarded the most medals? (top 10)
SELECT e.[discipline], COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [event] e ON oa.[event_id] = e.[id]
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY e.[discipline]
ORDER BY [medals] DESC
LIMIT 10;

-- How many medals were awarded at each Olympic edition?
SELECT oa.`year`, COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY oa.`year`
ORDER BY oa.`year`;

-- Which countries have won at least 100 medals all-time?
SELECT c.[name], COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY c.[name]
HAVING COUNT(*) >= 100
ORDER BY [medals] DESC;

------------------------------------------------------------------------
-- Athletes, events and venues
------------------------------------------------------------------------

-- Who are the top 10 athletes by number of medals won?
SELECT a.[first_name] || ' ' || a.[last_name] AS [athlete], c.[name] AS [country], COUNT(*) AS [medals]
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [medal] m ON oa.[medal_code] = m.[code]
GROUP BY a.[id], a.[first_name], a.[last_name], c.[name]
ORDER BY [medals] DESC
LIMIT 10;

-- How many athletes are there of each gender?
SELECT g.[gender], COUNT(*) AS [athletes]
FROM [athlete] a
JOIN [athlete_gender] g ON a.[athlete_gender_code] = g.[code]
GROUP BY g.[gender]
ORDER BY [athletes] DESC;

-- How many events does each discipline have? (top 10)
SELECT [discipline], COUNT(*) AS [events]
FROM [event]
GROUP BY [discipline]
ORDER BY [events] DESC
LIMIT 10;

-- List athletes who won a gold medal in swimming.
SELECT DISTINCT a.[first_name] || ' ' || a.[last_name] AS [athlete], c.[name] AS [country], oa.`year`
FROM [athlete_result] oa
JOIN [athlete] a ON oa.[athlete_id] = a.[id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
JOIN [event] e ON oa.[event_id] = e.[id]
JOIN [medal] m ON oa.[medal_code] = m.[code]
WHERE e.[discipline] = 'Swimming' AND m.[type] = 'Gold'
ORDER BY oa.`year`;

-- Stadium capacity statistics by country. (top 10 by average)
SELECT c.[name], COUNT(*) AS [stadiums], AVG(s.[seats]) AS [avgSeats], MAX(s.[seats]) AS [maxSeats]
FROM [stadium] s
JOIN [country] c ON s.[country_iso3] = c.[iso3]
GROUP BY c.[name]
ORDER BY [avgSeats] DESC
LIMIT 10;

-- The host city, country and continent of each edition.
SELECT o.`year`, o.[host_city], c.[name] AS [country], ct.[name] AS [continent]
FROM [olympic_games] o
JOIN [country] c ON o.[country_iso3] = c.[iso3]
JOIN [continent] ct ON c.[continent_id] = ct.[id]
ORDER BY o.`year`;

------------------------------------------------------------------------
-- Athlete physiology (height, weight, age)
------------------------------------------------------------------------

-- The 10 tallest medal-winning athletes (height and weight).
SELECT a.[first_name] || ' ' || a.[last_name] AS [athlete], c.[name] AS [country], a.[height_cm], a.[weight_kg]
FROM [athlete] a
JOIN [country] c ON a.[country_iso3] = c.[iso3]
WHERE a.[height_cm] IS NOT NULL
ORDER BY a.[height_cm] DESC
LIMIT 10;

-- The 10 youngest medallists (by birth date).
SELECT DISTINCT a.[first_name] || ' ' || a.[last_name] AS [athlete], c.[name] AS [country], a.[birth_date]
FROM [athlete_result] ar
JOIN [athlete] a ON a.[id] = ar.[athlete_id]
JOIN [country] c ON a.[country_iso3] = c.[iso3]
WHERE a.[birth_date] IS NOT NULL
ORDER BY a.[birth_date] DESC
LIMIT 10;

------------------------------------------------------------------------
-- Sponsors
------------------------------------------------------------------------

-- List the worldwide TOP sponsors of the 2004 (Athens) Olympics.
SELECT [sponsor_name] AS [sponsor]
FROM [olympic_games_sponsorship]
WHERE `year` = 2004
ORDER BY [sponsor_name];

-- How many worldwide TOP sponsors did each edition have?
SELECT `year`, COUNT(*) AS [sponsors]
FROM [olympic_games_sponsorship]
GROUP BY `year`
ORDER BY `year`;

------------------------------------------------------------------------
-- Using the database views
------------------------------------------------------------------------

-- Country medal counts per year and medal type (view: country_medals).
SELECT *
FROM [country_medals]
ORDER BY [medal_count] DESC
LIMIT 15;

-- Medal counts per continent and year (view: continent_medals).
SELECT *
FROM [continent_medals]
ORDER BY [medal_count] DESC
LIMIT 15;

-- Olympic sponsors with their host country (view: olympic_sponsors).
SELECT *
FROM [olympic_sponsors];
